/**
 * Created by ZFL on 2018/3/16.
 */
/**
 * Created by ZFL on 2017/11/29.
 */
/**
 * Created by ZFL on 2017/11/16.
 */
var vm=new Vue({
    el:'#getPacket',
    data:{
        Cashcoin:'' ,   //现金币
        mobile:'',      //手机号
        code:'',        //验证码
        isSendCode:true
    },
    methods: {
        sendCode:function () {
            if(this.isSendCode){
                sendCode();
            }
        },
        logging:function () {
            if (this.mobile==""){
                YDUI.dialog.toast("请先输入手机号码",'none',function(){});
            }
            else if (this.mobile.toString().length !=11) {
                YDUI.dialog.toast("手机号码格式不正确呀",'none',function(){});
            }
            else if (this.code=='') {
                YDUI.dialog.toast("请输入验证码",'none',function(){});
            }
            else if (this.code.toString().length<4) {
                YDUI.dialog.toast("验证码长度不低于4",'none',function(){});
            }else {
                getInfo();
            }
        }
    }
})
function appInit(){

}

function PageInit(){
   if($.getStorage('xmOpenid')){
       $.updataInfo($.getStorage('xmOpenid'))
   }
    showBg();
}

//点击拆红包 领取现金币
$('.removeRp').click(function(){
    sendCashcoin();   //发送现金币
})

var userId=0;
var terminalId= $.getPara('terminalId');
var uuId= $.getPara('uuId');
//领取现金币后绑定手机号
function goBindphone(){
    //如果是微信  就不出现绑定手机号并且是新用户
    if (isWeiXin()&&com_obj.userId!=0) {
        getCashcoin();
    //关注微信公众号
    $('.receiveRp').hide();
    $('.publicSign').show();
    }
    //else if(isWeiXin()&&com_obj.userId==0){
    //    getUserInfo()
    //}
    else{
        //绑定手机号
        $('.receiveRp').hide();
        $('.bindPhone').show();


    }
}

//$('.bindPhone b').click(function(){
//    $('.bindPhone').hide();
//    $('.publicSign').show();
//
//
//
//})

//发放现金币
function  sendCashcoin(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.terminalId=terminalId;
            obj.uuId=uuId;
            return obj;
        },
        visibleTime: 0,
        visible:'show',
        approve: true,
        api:"/yg_baijiaapp/userBank/getRandom",
        success:function(result){
            if(result.code==10000){
                var data=result.data;
                vm.Cashcoin=data.money;
                uuId=data.uuId
                $('.removeRp').hide();
                $('.receiveRp').show()
                //如果是微信端   由于有userId
                if(isWeiXin()){
                    userId=JSON.parse($.getStorage("xm_uData")).userId;
                }

            }else{
                YDUI.dialog.toast(result.message,1000)
            }
        }
    })
}



//发送短信验证码
function sendCode(e) {
    if ($("#phone").val() == ""){
        YDUI.dialog.toast("请先输入手机号码",'none',function(){});
    } else if ($("#phone").val().length != 11){
        YDUI.dialog.toast("手机号码格式不正确",'none',function(){});
    } else {
        $("#send-code-id").attr("disabled","disabled");
        $.ajaxHandler({
                model: function () {
                    var obj = {};
                    obj.mobile = $("#phone").val();
                    obj.type = 1;
                    return obj;
                },
                visibleTime: 100,
                dataType: "json",
                apiUrl:$.appApi(),
                api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                success: function (result) {
                    //console.log(result);
                    if (result.code == 10000) {
                        //app.isSendCode = false;
                        YDUI.dialog.toast('已发送', 'success', 1000);
                        SendTime("#send-code-id");
                    }else {
                        YDUI.dialog.toast(result.message,'none',function(){});
                    }

                }
            }
        );
    }
}

var wait = 60;//短信时间
function SendTime(parameter) {
    var Send = $("" + parameter + "");
    if (wait == 0) {
        //app.isSendCode = true;
        Send.html("点击获取验证码");
        wait = 60;
        $("#send-code-id").removeAttr("disabled");
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function () {
            SendTime(parameter);
        }, 1000);
    }
}




function getInfo() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.userName=vm.mobile;
            obj.code =vm.code;
            obj.type=1; //第三方登录类型1APP 2H5
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
//          apiUrl:$.appApi(),
        api: 'yg_baijiaapp/user/dynamicRegister',
        success: function (result) {
            if (result.code == 10000){
                var data=result.data;
                userId=data.userId;
                if(isWeiXin()){
                    var openUrl = window.location.href;
                    window.localStorage.setItem("openUrl",openUrl)
                    saveInfo(result.data,1);
                }else{

                    var _obj  ={};
                    //console.log(data);
                    _obj.token = data.token;
                    $.setStorage("xm_uData",_obj);
                }
                getCashcoin();
                //if(vm.Cashcoin>0){
                //    YDUI.dialog.toast('领取成功',1000)
                //
                //}else if(vm.Cashcoin==-5){
                //    YDUI.dialog.toast('UUID无效',1000)
                //}else if(vm.Cashcoin==-6){
                //    YDUI.dialog.toast('当天此店铺领取已达上限',1000)
                //}
                //$('.bindPhone').hide();
                //$('.publicSign').show();
            } else {
                YDUI.dialog.toast(result.message,'error',1000);
            }
        }
    });
}
//真正发放现金币
function  getCashcoin(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.channel=2;
            obj.terminalId=terminalId;
            obj.uuId=uuId;
            obj.userId=userId;
            return obj;
        },
        visibleTime: 0,
        visible:'show',
        approve: true,
        api:"/yg_baijiaapp/userBank/luckyPrize",
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                if(data>0){
                    vm.Cashcoin=res.data;
                    YDUI.dialog.toast('领取成功',1000)
                }else if(data==-5){
                    vm.Cashcoin=res.data;
                    YDUI.dialog.toast('UUID无效',1000)
                }else if(data==-6){
                    vm.Cashcoin=res.data;
                    YDUI.dialog.toast('当天此店铺领取已达上限',1000)
                }

                if(isWeiXin()){
                    $('.bindPhone').hide();
                    $('.receiveRp').hide();
                    $('.publicSign').show();
                }else{
                    setTimeout(function(){
                        window.location.href=$.wxApi()+'?status=0';
                    },1000)

                }

                //$.delStorage('xm_uData');
                //$.delStorage('xmOpenid')
                //$.delStorage("hx_wx");
                //$.delStorage("hx_AccessTokenIndex");
                //$.delStorage("wx");
                //$.delStorage("uData");
                //$.delStorage("AccessTokenIndex");


            }else{
                alert(JSON.stringify(res))
                //YDUI.dialog.toast(res.data,1000)
            }
        }
    })
}

//显示灰色 jQuery 遮罩层
function showBg() {
    var bh = $(window).height();
    var bw = $(window).width();
    $("#fullbg").css({
        height:bh,
        width:bw,
        display:"block",
    });
    $("#dialog").show();
    $('body').css("overflow","hidden")

}
//关闭灰色 jQuery 遮罩
function closeBg(){
    $("#fullbg,#dialog").hide();
    $('body').css("overflow","visible")
//        location.href ='/b2c/app/downloadApp.html';
}
$('#fullbg').click(function(){
    $('#fullbg').hide();
    $('.removeRp').hide();
    $('.receiveRp').hide();
    $('.bindPhone').hide();
    $('.publicSign').hide();
})